

#include "stdafx.h"
#include "ReadXml.h"




CReadXml::CReadXml()
{
}

CReadXml::~CReadXml()
{
}


void CReadXml::ReadXml(CString path,	CLayout &layout)
{
	CString mainPath, wallPath, holderPath;
	mainPath = path + _T("mainwin.xml");
	wallPath = path + _T("wallwin.xml");
	holderPath = path + _T("holderwin.xml");

	ReadMainWndXml(mainPath, layout);
	ReadWallWndXml(wallPath, layout);
	ReadHolderWndXml(holderPath, layout);
}


void CReadXml::ReadMainWndXml(CString xmlName, CLayout &layout)
{
	BOOL bLoadXml = FALSE;
	CMyMarkup xml;
	bLoadXml = xml.Load(xmlName);
	if (!bLoadXml)
		Print("Load err!");


	xml.ResetMainPos();

	xml.FindElem(); //WINDOW

	MainWinLayout& theLayout = layout.GetMainWndLayout();

	GetAttribute(xml, _T("pos"), theLayout.winCof.pos);
	GetAttribute(xml, _T("size"), theLayout.winCof.size);
	GetAttribute(xml, _T("bkcolor"), theLayout.winCof.bkColor[0]);
	GetAttribute(xml, _T("bkcolor1"), theLayout.winCof.bkColor[1]);

	xml.IntoElem();
	xml.FindElem(); //TITLE
	GetAttribute(xml, _T("pos"), theLayout.titleCof.pos);
	GetAttribute(xml, _T("size"), theLayout.titleCof.size);

	xml.IntoElem();
	xml.FindElem(); //RGM
	GetAttribute(xml, _T("point1"), theLayout.rgnCof[0].point[0]);
	GetAttribute(xml, _T("point2"), theLayout.rgnCof[0].point[1]);
	GetAttribute(xml, _T("point3"), theLayout.rgnCof[0].point[2]);
	GetAttribute(xml, _T("point4"), theLayout.rgnCof[0].point[3]);
	GetAttribute(xml, _T("bkcolor"), theLayout.rgnCof[0].bkColor);

	xml.IntoElem();
	xml.FindElem(); //TEXT
	GetAttribute(xml, _T("pos"), theLayout.textCof[0].pos);
	GetAttribute(xml, _T("size"), theLayout.textCof[0].size);
	GetAttribute(xml, _T("textcolor"), theLayout.textCof[0].textColor);
	GetAttribute(xml, _T("fontsize"), theLayout.textCof[0].fontSize);

	xml.OutOfElem();
	xml.FindElem();	//RGN
	GetAttribute(xml, _T("point1"), theLayout.rgnCof[1].point[0]);
	GetAttribute(xml, _T("point2"), theLayout.rgnCof[1].point[1]);
	GetAttribute(xml, _T("point3"), theLayout.rgnCof[1].point[2]);
	GetAttribute(xml, _T("point4"), theLayout.rgnCof[1].point[3]);
	GetAttribute(xml, _T("bkcolor"), theLayout.rgnCof[1].bkColor);

	xml.IntoElem();
	xml.FindElem(); //TEXT
	GetAttribute(xml, _T("pos"), theLayout.textCof[1].pos);
	GetAttribute(xml, _T("size"), theLayout.textCof[1].size);
	GetAttribute(xml, _T("textcolor"), theLayout.textCof[1].textColor);
	GetAttribute(xml, _T("fontsize"), theLayout.textCof[1].fontSize);

	xml.OutOfElem();
	xml.FindElem();	//RGN
	GetAttribute(xml, _T("point1"), theLayout.rgnCof[2].point[0]);
	GetAttribute(xml, _T("point2"), theLayout.rgnCof[2].point[1]);
	GetAttribute(xml, _T("point3"), theLayout.rgnCof[2].point[2]);
	GetAttribute(xml, _T("point4"), theLayout.rgnCof[2].point[3]);
	GetAttribute(xml, _T("bkcolor"), theLayout.rgnCof[2].bkColor);

	xml.IntoElem();
	xml.FindElem(); //TEXT
	GetAttribute(xml, _T("pos"), theLayout.textCof[2].pos);
	GetAttribute(xml, _T("size"), theLayout.textCof[2].size);
	GetAttribute(xml, _T("textcolor"), theLayout.textCof[2].textColor);
	GetAttribute(xml, _T("fontsize"), theLayout.textCof[2].fontSize);

	xml.FindElem();	//IMAGE
	GetAttribute(xml, _T("pos"), theLayout.uFlash.pos);
	theLayout.uFlash.path = GetAttribute(xml, _T("path"));

}

void CReadXml::ReadWallWndXml(CString xmlName, CLayout &layout)
{
	BOOL bLoadXml = FALSE;
	CMyMarkup xml;
	bLoadXml = xml.Load(xmlName);
	if (!bLoadXml)
		ASSERT("load err!");

	xml.ResetMainPos();

	WallWndLayout& theLayout = layout.GetWallWndLayout();

	xml.FindElem(); //WINDOW
	GetAttribute(xml, _T("pos"), theLayout.winCof.pos);
	GetAttribute(xml, _T("size"), theLayout.winCof.size);
	GetAttribute(xml, _T("bkcolor"), theLayout.winCof.bkColor[0]);

	xml.IntoElem();

	int i = 0;
	while (xml.FindElem()) {
		GetAttribute(xml, _T("pad_left"), theLayout.holderCof[i].pad_left);
		GetAttribute(xml, _T("pad_top"), theLayout.holderCof[i].pad_top);
		GetAttribute(xml, _T("pad_between"), theLayout.holderCof[i].pad_between);
		GetAttribute(xml, _T("size"), theLayout.holderCof[i].size);
		theLayout.holderCof[i].gifPath = GetAttribute(xml, _T("gif"));
		i++;
	}
}

void CReadXml::ReadHolderWndXml(CString xmlName, CLayout &layout)
{
	BOOL bLoadXml = FALSE;
	CMyMarkup xml;
	bLoadXml = xml.Load(xmlName);
	if (!bLoadXml)
		ASSERT("load err!");

	xml.ResetMainPos();

	HolderWndLayout& theLayout = layout.GetHolderWndLayout();

	xml.FindElem(); //WINDOW
	GetAttribute(xml, _T("pos"), theLayout.winCof.pos);
	GetAttribute(xml, _T("size"), theLayout.winCof.size);
	GetAttribute(xml, _T("bkcolor"), theLayout.winCof.bkColor[0]);

	xml.IntoElem();
	xml.FindElem();	//TITLE
	GetAttribute(xml, _T("height"), theLayout.titleCof.height);
	GetAttribute(xml, _T("bkcolor"), theLayout.titleCof.bkColor);

	xml.IntoElem();
	xml.FindElem();	//Image_talking
	GetAttribute(xml, _T("pos"), theLayout.image_talking.pos);
	theLayout.image_talking.path = GetAttribute(xml, _T("path"));

	xml.FindElem();	//Camera name
	GetAttribute(xml, _T("signelpos"), theLayout.cameraname_signel.pos);
	GetAttribute(xml, _T("largepos"), theLayout.cameraname_large.pos);
	GetAttribute(xml, _T("size"), theLayout.cameraname_large.size);
	GetAttribute(xml, _T("size"), theLayout.cameraname_signel.size);
	GetAttribute(xml, _T("textcolor"), theLayout.cameraname_large.textColor);
	GetAttribute(xml, _T("textcolor"), theLayout.cameraname_signel.textColor);
	GetAttribute(xml, _T("fontsize"), theLayout.cameraname_large.fontSize);
	GetAttribute(xml, _T("fontsize"), theLayout.cameraname_signel.fontSize);

	xml.FindElem(); //Talk_Status
	GetAttribute(xml, _T("pad_right"), theLayout.talk_status.pad[2]);
	GetAttribute(xml, _T("textcolor"), theLayout.talk_status.textColor);
	GetAttribute(xml, _T("fontsize"), theLayout.talk_status.fontSize);

	xml.FindElem(); //Image_miss
	theLayout.image_miss.path = GetAttribute(xml, _T("path"));

	xml.FindElem();	//Image_request
	theLayout.image_request.path = GetAttribute(xml, _T("path"));

	xml.OutOfElem();
	xml.FindElem(); //FuckBtn
	GetAttribute(xml, _T("pad_between"), theLayout.FuckBtn.pad_between);
	GetAttribute(xml,_T("size"),theLayout.FuckBtn.size);
	GetAttribute(xml, _T("fontsize"), theLayout.FuckBtn.fontSize);

	xml.FindElem(); //Border

	GetAttribute(xml, _T("size"), theLayout.border.size);
	GetAttribute(xml, _T("color"), theLayout.border.color);
}

void CReadXml::ReadStaticTextInfo(CString path, CStaticText & text)
{
	CString stringPath;
	stringPath = path + _T("string.xml");
	BOOL bLoadXml = FALSE;
	CMyMarkup xml;
	bLoadXml = xml.Load(STRING_XML_PATH);
	if (!bLoadXml)
		ASSERT("load err!");

	StaticText& textInfo = text.GetStaticText();

	xml.ResetMainPos();

	//MonitorStatus
	xml.FindElem(); 
	xml.IntoElem();

	//AutoWatchOn
	xml.FindElem();
	textInfo.mMonitorStatus.mAwOn = xml.GetData();
	//AutoWatchOff
	xml.FindElem();
	textInfo.mMonitorStatus.mAwOff = xml.GetData();
	//Alarming
	xml.FindElem();
	textInfo.mMonitorStatus.mAlarming = xml.GetData();

	xml.OutOfElem();

	//HomeWatchStatus
	xml.FindElem();	
	xml.IntoElem();
	//HomeWatchOn
	xml.FindElem();
	textInfo.mHomeWatchStatus.mHomeWatchOn = xml.GetData();
	//HomeWatchOff
	xml.FindElem();
	textInfo.mHomeWatchStatus.mHomeWatchOff = xml.GetData();

	xml.OutOfElem();

	//PortName
	xml.FindElem();
	xml.IntoElem();

	int i = 0;
	while (xml.FindElem()) {
		textInfo.mPortName.mName[i+1] = xml.GetData();
		i++;
	}

	CString test;
	test = textInfo.mPortName.mName[2];

	xml.OutOfElem();

	//TalkStatus
	xml.FindElem();
	xml.IntoElem();
	//TalkFault
	xml.FindElem();
	textInfo.mTalkStatus.mTalkFault = xml.GetData();
	//TalkReq
	xml.FindElem();
	textInfo.mTalkStatus.mTalkReq = xml.GetData();

}


CString CReadXml::GetAttribute(CMyMarkup xml, CString pstrName)
{
	CString pstrValue;
	LPTSTR pstr = NULL;
	pstrValue = xml.GetAttrib(pstrName);
	return pstrValue;
}

void CReadXml::GetAttribute(CMyMarkup xml, CString pstrName, CPoint &point)
{
	CString pstrValue;
	LPTSTR pstr = NULL;
	pstrValue = xml.GetAttrib(pstrName);
	point.x = _tcstol(pstrValue, &pstr, 10);
	point.y = _tcstol(pstr + 1, &pstr, 10);
}

void CReadXml::GetAttribute(CMyMarkup xml, CString pstrName, CSize & size)
{
	CString pstrValue;
	LPTSTR pstr = NULL;
	pstrValue = xml.GetAttrib(pstrName);
	size.cx = _tcstol(pstrValue, &pstr, 10);
	size.cy = _tcstol(pstr + 1, &pstr, 10);
}

void CReadXml::GetAttribute(CMyMarkup xml, CString pstrName, DWORD &value)
{
	CString pstrValue;
	LPTSTR pstr = NULL;
	pstrValue = xml.GetAttrib(pstrName);
	value = _tcstoul(pstrValue, &pstr, 16);
}

void CReadXml::GetAttribute(CMyMarkup xml, CString pstrName, CRect & value)
{
	CString pstrValue;
	LPTSTR pstr = NULL;
	pstrValue = xml.GetAttrib(pstrName);
	value.left = _tcstoul(pstrValue, &pstr, 10);
	value.top = _tcstoul(pstr +1, &pstr, 10);
	value.right = _tcstoul(pstr + 1, &pstr, 10);
	value.bottom = _tcstoul(pstr + 1, &pstr, 10);
}

void CReadXml::GetAttribute(CMyMarkup xml, CString pstrName, int  &value)
{
	CString pstrValue;
	LPTSTR pstr = NULL;
	pstrValue = xml.GetAttrib(pstrName);
	value = _tcstoul(pstrValue, &pstr, 10);
}




//void CReadXmlApp::CreateXml()
//{
//	CMyMarkup xml;
//	xml.SetDoc(_T("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"));
//	xml.AddElem(_T("School"));
//	xml.IntoElem();
//	xml.AddElem(_T("Department"), _T("Automation"));
//	xml.AddElem(_T("Department"), _T("Computer"));
//	xml.AddElem(_T("Department"), _T("Math"));
//	xml.AddElem(_T("Department"), _T("English"));
//	xml.OutOfElem();
//	xml.Save(_T("D:\\School.xml"));
//}